<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<!-- 筛选状态 -->
			<cl-filter label="状态">
				<cl-select prop="status" :options="options.status" />
			</cl-filter>
			<!-- 筛选类型 -->
			<cl-filter label="类型">
				<cl-select prop="type" :options="options.type" />
			</cl-filter>
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key placeholder="搜索标题、描述" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="bannersetting-banner" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { reactive } from "vue";

const { service } = useCool();

// 选项
const options = reactive({
	status: [
		{ label: "禁用", value: 0, type: "danger" },
		{ label: "启用", value: 1, type: "success" }
	],
	type: [
		{ label: "普通", value: 0, type: "danger" },
		{ label: "特殊", value: 1, type: "success" }
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: "标题",
			prop: "title",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{ label: "图片", prop: "image", component: { name: "cl-upload" }, required: true },
		{
			label: "链接",
			prop: "link",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		},
		{
			label: "排序",
			prop: "sort",
			hook: "number",
			component: { name: "el-input-number" },
			required: true
		},
		{
			label: "状态",
			prop: "status",
			component: { name: "el-radio-group", options: options.status },
			value: 0,
			required: true
		},
		{
			label: "类型",
			prop: "type",
			component: { name: "el-radio-group", options: options.type },
			value: 0,
			required: true
		},
		{
			label: "描述",
			prop: "description",
			component: { name: "el-input", props: { type: "textarea", rows: 4 } }
		},
		{
			label: "时间范围",
			prop: "time",
			component: {
				name: "el-date-picker",
				props: {
					type: "datetimerange",
					valueFormat: "YYYY-MM-DD HH:mm:ss",
					defaultTime: ["2000-01-31T16:00:00.000Z", "2000-02-01T15:59:59.000Z"]
				}
			},
			required: true,
			hook: "datetimeRange"
		},
		{
			label: "位置",
			prop: "position",
			component: { name: "el-input", props: { clearable: true } },
			required: true
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "标题", prop: "title", minWidth: 140 },
		{
			label: "图片",
			prop: "image",
			minWidth: 100,
			component: { name: "cl-image", props: { size: 60 } }
		},
		{ label: "链接", prop: "link", minWidth: 140 },
		{ label: "排序", prop: "sort", minWidth: 140 },
		{ label: "状态", prop: "status", dict: options.status, minWidth: 120 },
		{ label: "类型", prop: "type", dict: options.type, minWidth: 120 },
		{ label: "描述", prop: "description", showOverflowTooltip: true, minWidth: 200 },
		{
			label: "开始时间",
			prop: "startTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "结束时间",
			prop: "endTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{ label: "位置", prop: "position", minWidth: 140 },
		{
			label: "更新时间",
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" }
		},
		{
			label: "创建时间",
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" }
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.bannersetting.banner
	},
	(app) => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
